﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.Core;
using EPiSugar.Navigation;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using EPiSugar.UnitTesting;


namespace EPiSugar.Tests.Navigation
{
    [TestFixture]
    public class FilterTests : NavigationTestsBase
    {
        [Test]
        public void Assert_PageTypeID_Expectations()
        {
            Assert.That(rootPage.PageTypeID, Is.EqualTo(Fakes.RootPageTypeID));
            Assert.That(wasteBasket.PageTypeID, Is.EqualTo(Fakes.WastebasketTypeID));
            Assert.That(startPage.PageTypeID, Is.EqualTo(Fakes.StartPageTypeID));
            Assert.That(startPage.Children().First().PageTypeID, Is.EqualTo(Fakes.NormalPageTypeID));
        }

        [Test]
        public void CanFilter_PageChildren_WithLambda()
        {
            var pages = rootPage.Children(x => x.PageTypeID == Fakes.StartPageTypeID);
            Assert.That(pages.Count(), Is.EqualTo(1));
            Assert.That(pages.First(), Is.EqualTo(startPage));
        }

        [Test]
        public void CanFilter_PageChildren_ByPageTypeID()
        {
            var pages = rootPage.Children(Filter.By.PageType(Fakes.StartPageTypeID));
            Assert.That(pages.Count(), Is.EqualTo(1));
            Assert.That(pages.First(), Is.EqualTo(startPage));
        }

        [Test]
        public void CanFilter_PageChildren_ByPageTypeName()
        {
            var pages = rootPage.Children(Filter.By.PageType("PageType" + Fakes.StartPageTypeID));
            Assert.That(pages.Count(), Is.EqualTo(1));
            Assert.That(pages.First(), Is.EqualTo(startPage));
        }

        [Test]
        public void CanFilter_PageChildren_ByPageVisible()
        {
            var pages = rootPage.Children(Filter.By.Visible());
            Assert.That(pages.Count(), Is.EqualTo(1));
            Assert.That(pages.Contains(startPage));
        }

        [Test]
        public void CanFilter_PageChildren_ByPageNotVisible()
        {
            var pages = rootPage.Children(Filter.By.NotVisible());
            Assert.That(pages.Count(), Is.EqualTo(1));
            Assert.That(pages.Contains(wasteBasket));
        }

        [Test]
        public void CanFilter_PageDescendants_ByPageParentLink()
        {
            var pages = rootPage.Descendants(Filter.By.Parent(rootPage.PageLink));
            Assert.That(pages.Count(), Is.EqualTo(2));
            Assert.That(pages.Contains(wasteBasket) && pages.Contains(startPage));
        }

        [Test]
        public void CanFilter_PageDescendants_ByPageParent()
        {
            var pages = rootPage.Descendants(Filter.By.Parent(rootPage));
            Assert.That(pages.Count(), Is.EqualTo(2));
            Assert.That(pages.Contains(wasteBasket) && pages.Contains(startPage));
        }

        [Test]
        public void CanFilter_ByPublished_NotPublishedYet()
        {
            PageData unpublished = startPage.Children().First();
            unpublished.Property.Add("PageStartPublish", new PropertyDate(DateTime.Now.AddSeconds(10)));

            var publishedChildren = startPage.Children(Filter.By.Published());
            Assert.That(publishedChildren.Count(), Is.EqualTo(2));

            unpublished.Property.Remove("PageStartPublish");
        }

        [Test]
        public void CanFilter_ByPublished_Expired()
        {
            PageData unpublished = startPage.Children().First();
            unpublished.Property.Add("PageStopPublish", new PropertyDate(DateTime.Now.AddSeconds(-10)));

            var publishedChildren = startPage.Children(Filter.By.Published());
            Assert.That(publishedChildren.Count(), Is.EqualTo(2));

            unpublished.Property.Remove("PageStartPublish");
        }
    }
}
